﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;

namespace Frogism.Web
{
    public class GlobalApplication : System.Web.HttpApplication
    {
      protected static void RegisterRoutes(RouteCollection routes)
        {
            // Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
            //       automatic support on IIS6 and IIS7 classic mode

            routes.Add(new Route("{controller}.ctc/{action}/{id}", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { action = "Index", id = "" }),
            });

            routes.Add(new Route("Default.aspx", new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),
            });
        }

        /*
        public void RegisterRoutes(RouteCollection routes)
        {

              routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" },
                new {controller=@"[^\.]*"});
        }*/

        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }
        protected void Application_AuthenticateRequest(Object sender,  EventArgs e  ){

       // '' Fires upon attempting to authenticate the use
        String cookieName   = FormsAuthentication.FormsCookieName;
        HttpCookie authCookie   = Request.Cookies[cookieName];
        if (authCookie ==null) {
            //'authCookie = GetCookie() '' for Windows Authentication
            return ; //'' for Forms Authentication
            }
        FormsAuthenticationTicket authTicket;
        try{
            authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        }catch(Exception ex){
            return;
        }
        
        if( authTicket == null){
            return;
        }
        String roles ;
        roles = authTicket.UserData;
        FormsIdentity id  = new FormsIdentity(authTicket);
        var principal = new FrogPrincipal(id);
        Context.User = principal;
        }
    private HttpCookie GetCookie(){
        Core.User user ;
        String username ; 
        username = Request.ServerVariables["AUTH_USER"];
        if(username.IndexOf('\\') > 0){
            username = username.Split('\\')[1];
        }
       

        user = (new Domain.UserManager()).GetUser(username);

       if (user == null) {
            throw new Exception("User Not Found");
        }
        

        //'        Dim roles As String = "Admin"
        FormsAuthenticationTicket authTicket;
        authTicket = new FormsAuthenticationTicket(1, user.Name, DateTime.Now, DateTime.Now.AddMinutes(20), false, "");
        String encryptedTicket ; 
        encryptedTicket = FormsAuthentication.Encrypt(authTicket);
        HttpCookie authCookie  ;
        authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
        Response.Cookies.Add(authCookie);

        return authCookie;
    }
    }
}